import Vue from 'vue'

import App from './App'

import router from './router'

import './assets/css/index.scss'

import constVar from './global/const/index'

import 'element-ui/lib/theme-chalk/index.css'

import element from 'element-ui'

import VueTouch from 'vue-touch'

import jsPlumb from 'jsplumb'
Vue.prototype.$jsPlumb = jsPlumb.jsPlumb

import loadingmask from './components/loadingmask/loadingmask'
import mydialog from './components/dialog/dialog'
import VCharts from 'v-charts'
require('./global/ini/index.js')

if (!process.env.IS_WEB) Vue.use(require('vue-electron'))

Vue.config.productionTip = false

Vue.prototype.$loadingmask = loadingmask

Vue.prototype.$dialog = mydialog

Vue.use(VueTouch, {
  name: 'v-touch',
})

Vue.use(element)
Vue.use(VCharts)

Vue.prototype.$CONST = constVar
/* eslint-disable no-new */
let lockArouse = false
const path = require('path')
const exePath = process.env.NODE_ENV !== 'production' ? path.join(process.cwd(), 'bos_builder', 'ExecOSK.exe') : 'ExecOSK.exe'
const arouseFun = function(a) {
  const arouse = window.localStorage.getItem('arouse')
  if (arouse === 'true' && !lockArouse) {
    const exec = require('child_process').exec
    lockArouse = true
    exec(exePath, (err) => {
      //成功执行的回调
      if (!err) {
        lockArouse = false
      }
    })
  }
}
const arouseFunTouch = function(e) {
  const arouse = window.localStorage.getItem('arouse')
  if (arouse === null) {
    Vue.prototype
      .$dialog({
        title: '温馨提示',
        confirm: '开启',
        cancel: '取消',
        message: '检测到您正在使用触摸屏，请问是否开启系统软键盘？开启后我们将自动为您打开软键盘，不需要此功能时可在设置中关闭。',
      })
      .then((_) => {
        window.localStorage.setItem('arouse', true)
        e.target.focus()
        const exec = require('child_process').exec
        exec(exePath)
      })
      .catch((_) => {
        window.localStorage.setItem('arouse', false)
      })
  } else {
    if (arouse === 'true' && !lockArouse) {
      const exec = require('child_process').exec
      lockArouse = true
      exec(exePath, (err) => {
        //成功执行的回调
        if (!err) {
          lockArouse = false
        }
      })
    }
  }
}
Vue.directive('arouse', {
  bind(el, binding) {
    el.addEventListener('touchstart', arouseFunTouch)
    el.addEventListener('mousedown', arouseFun)
  },
  // inserted() {
  //   console.log(arguments);
  // },
  // update() {
  //   console.log(arguments);
  // },
  // componentUpdated() {
  //   console.log(arguments);
  // },
  unbind(el) {
    el.removeEventListener('mousedown', arouseFun)
    el.removeEventListener('touchstart', arouseFun)
  },
})

new Vue({
  el: '#app',
  router,
  components: {
    App,
  },
  template: '<App/>',
  created() {},
})
